/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uvexposureapplication.main;

import java.util.Observable;
import java.util.Observer;
import uvexposureapplication.eventrouting.Consumer;
import uvexposureapplication.eventrouting.elvin.ElvinConsumer;
import uvexposureapplication.eventrouting.elvin.notificationlistener.ServerNotificationListener;
import uvexposureapplication.rpc.AbstractPseudoRPCClientStub;
import uvexposureapplication.rpc.clients.ContextManagerPseudoRPCClientStub;
import uvexposureapplication.utils.IOStream;

/**
 *
 * @author matthieukluj
 */
public class UVAppUI implements Observer {

    private static Consumer consumer;
    private static String name;
    private static char option;
    public ServerNotificationListener ntf;
    public AbstractPseudoRPCClientStub contextManagerPseudoRPCClientStub;
    public AbstractPseudoRPCClientStub contextManagerPseudoRPCClientStub2;

    public UVAppUI(String server) {
        this.ntf = new ServerNotificationListener();
        this.ntf.addObserver(this);

        this.consumer = new ElvinConsumer(server, this.ntf);
    }

    /**·
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 1) {
            System.out.println("usage: java UVAppUI [ElvinURL]");
            System.exit(0);
        }

        UVAppUI uvAppUI = new UVAppUI(args[0]);

        //consumer = new ElvinConsumer(args[0]);
        System.out.println("Location-aware UV Exposure Application");
        System.out.println("Please enter your user name:");
        name = IOStream.readLine();
        uvAppUI.consumer.subscribe("Name == '" + name + "'");

        while (true) {

            System.out.println("\nLocation-aware UV Exposure Application Main Menu");
            System.out.println("Please select an option:");
            System.out.println("1. Search for information on a specific item of interest");
            System.out.println("2. Search for items of interest in current location");
            System.out.println("E. Exit");

            option = IOStream.readLine().charAt(0);

            switch (option) {
                case '1':
                    System.out.println("Enter the name of an item of interest: \n");
                    String item = IOStream.readLine();
                    uvAppUI.contextManagerPseudoRPCClientStub = new ContextManagerPseudoRPCClientStub(uvAppUI, "searchInfos", item, args[0]);
                    uvAppUI.contextManagerPseudoRPCClientStub.run();
                    break;
                case '2':
                    uvAppUI.contextManagerPseudoRPCClientStub = new ContextManagerPseudoRPCClientStub(uvAppUI, "searchItemsInLocation", name , args[0]);
                    uvAppUI.contextManagerPseudoRPCClientStub.run();
                    break;
                case 'E':
                    System.out.println("Bye bye");
                    System.exit(1);
                    break;
                default:
                    System.out.println("Enter a Valid option");
                    break;
            }

        }
    }
    
    public void printItemInformation(String item) {
        System.out.println(item);
    }
    
    public void printItemInLocation(String item) {
        System.out.println(item);
    }

    @Override
    public void update(Observable o, Object o1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
